import Vue from 'vue'
import Router, {RouteConfig} from 'vue-router'
import Layout from '@/core/layouts/Layout.vue'

Vue.use(Router);

/*
  name:'router-name'             使用<keep-alive>时，name字段是必填字段，它也应与其组件的name属性匹配
  redirect:                      如果设置为“ noredirect”，则在单击面包屑时将不会触发任何重定向操作
  meta: {
    title: 'title'               子菜单和面包屑中显示的名称（推荐设置）
    icon: 'svg-name'             侧栏中显示的图标
    hidden: true                 如果为true，则此路由不会显示在边栏中（默认为false）

    alwaysShow: true             如果为true，将始终显示根菜单（默认为false）
                                 如果为false，则当子路由小于或等于一个时，隐藏根菜单

    breadcrumb: false            如果为false，则该项将隐藏在面包屑中（默认为true）
    noCache: true                如果为true，将不缓存页面（默认为false）
    affix: true                  如果为true，则标签将粘贴在标签视图中
    activeMenu: '/example/list'  如果设置了路径，则侧边栏将突出显示您设置的路径
  }
*/

/**
 ConstantRoute路由没有权限要求的基本页面，可以访问所有角色
 */
export const constantRoutes: RouteConfig[] = [
    {
        path: '/redirect',
        component: Layout,
        meta: {hidden: true},
        children: [
            {
                path: '/redirect/:path(.*)',
                name: 'Redirect',
                component: () => import('@/views/Redirect.vue')
            }
        ]
    },
    {
        path: '/login',
        name: "Login",
        component: () => import('@/views/Login.vue'),
        meta: {hidden: true}
    },
    {
        path: '/404',
        component: () => import('@/views/hello.vue'),
        meta: {hidden: true}
    },
    {
        path: '/401',
        component: () => import('@/views/hello.vue'),
        meta: {hidden: true}
    },
    {
        path: '/',
        component: Layout,
        redirect: '/dashboard',
        children: [
            {
                path: 'dashboard',
                component: () => import('@/views/hello.vue'),
                name: 'Dashboard',
                meta: {
                    title: 'dashboard',
                    icon: 'dashboard',
                    affix: true
                }
            }
        ]
    },
    {
        path: '/guide',
        component: Layout,
        redirect: '/guide/index',
        children: [
            {
                path: 'index',
                component: () => import('@/views/hello.vue'),
                name: 'Guide',
                meta: {
                    title: 'guide',
                    icon: 'guide',
                    noCache: true
                }
            }
        ]
    },
    {
        path: '/system',
        component: Layout,
        redirect: '/system/dict',
        meta: {type: "1", title: '系统管理', icon: 'excel'},
        children: [
            //用户
            {
                path: '/system/user/list',
                component: () => import('@/views/system/user/list.vue'),
                name: 'UserList',
                meta: {type: "2", icon: 'excel', title: '用户管理', sort: '1000'},
                children: [{
                    path: '/system/user/insert',
                    component: () => import('@/views/system/user/insert.vue'),
                    name: 'UserInsert',
                    meta: {type: "2", icon: 'excel', title: '添加用户', sort: '1000', hidden: "2"}
                }, {
                    path: '/system/user/edit',
                    component: () => import('@/views/system/user/edit.vue'),
                    name: 'UserEdit',
                    meta: {type: "2", icon: 'excel', title: '编辑用户', sort: '1000', hidden: "2"}
                }, {
                    path: '/system/user/info',
                    component: () => import('@/views/system/user/info.vue'),
                    name: 'UserInfo',
                    meta: {type: "2", icon: 'excel', title: '用户详情', sort: '1000', hidden: "2"}
                }]
            }
        ]
    },
];
const createRouter = () => new Router({
    mode: 'history',
    scrollBehavior: (to, from, savedPosition) => {
        if (savedPosition) {
            return savedPosition
        } else {
            return {x: 0, y: 0}
        }
    },
    base: process.env.BASE_URL,
    routes: constantRoutes
});

const router = createRouter();

export function resetRouter() {
    const newRouter = createRouter();
    (router as any).matcher = (newRouter as any).matcher
}

export default router
